/*
 * IngresoKLambdas.java
 *
 * Created on 15-may-2012, 10:20:26
 */
package view;

import controller.MainController;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author DAVINCI
 */
public class IngresoKLambdas extends javax.swing.JFrame {

    private int nFilas;
    private int mColumnas;
    private TableModel tModelFilasK;
    private TableModel tModelFilasL;
    private TableModel tModelColumnasK;
    private TableModel tModelColumnasL;
    private TableModel tModelTodasK;
    private TableModel tModelTodasL;
    private TableModel tModelUnaK;
    private TableModel tModelUnaL;
    private int inicioK [];
    private Principal p;
    
    
    Double valorL [];
    int valorK;
    ArrayList<Double> valorLD [][];
    int kD[][];
    
    /** Creates new form IngresoKLambdas */
    public IngresoKLambdas(int nFilas, int mColumnas, Principal p) {
        
        this.mColumnas = mColumnas;
        this.nFilas = nFilas;
        this.p=p;

        initComponents();

        buttonGroup.add(radiobK);
        buttonGroup.add(radiobLambda);

        radiobK.setSelected(true);

        tModelTodasK = new DefaultTableModel(
                new Object[nFilas * mColumnas][2],
                new String[]{"Celda", "K"
                });

        //Se agrega modelo por default a la tabla [n*m] x [6]
        tableKLambdas.setModel(tModelTodasK);


        int aux = 0;
        for (int i = 0; i < nFilas; i++) {
            for (int j = 0; j < mColumnas; j++) {
                String celda = "(" + i + "," + j + ")";
                tableKLambdas.setValueAt(celda, aux, 0);
                aux++;
            }

        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup = new javax.swing.ButtonGroup();
        tableScroll = new javax.swing.JScrollPane();
        tableKLambdas = new javax.swing.JTable();
        buttonAceptar = new javax.swing.JButton();
        checkRepetirFilas = new javax.swing.JCheckBox();
        checkRepetirColumnas = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        radiobK = new javax.swing.JRadioButton();
        radiobLambda = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tableKLambdas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableKLambdas.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tableKLambdasFocusLost(evt);
            }
        });
        tableScroll.setViewportView(tableKLambdas);

        buttonAceptar.setText("Aceptar");
        buttonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAceptarActionPerformed(evt);
            }
        });

        checkRepetirFilas.setText("Filas");
        checkRepetirFilas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkRepetirFilasActionPerformed(evt);
            }
        });

        checkRepetirColumnas.setText("Columnas");
        checkRepetirColumnas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkRepetirColumnasActionPerformed(evt);
            }
        });

        jLabel1.setText("Repetir valores por:");

        radiobK.setText("K's");
        radiobK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radiobKActionPerformed(evt);
            }
        });

        radiobLambda.setText("Lambdas");
        radiobLambda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radiobLambdaActionPerformed(evt);
            }
        });

        jLabel2.setText("Ingresar:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tableScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(buttonAceptar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel1)
                                .addGap(52, 52, 52))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(radiobK)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(radiobLambda)
                                .addGap(78, 78, 78)
                                .addComponent(checkRepetirFilas)
                                .addGap(18, 18, 18)
                                .addComponent(checkRepetirColumnas)))))
                .addGap(0, 149, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(radiobK)
                            .addComponent(radiobLambda))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(22, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(checkRepetirFilas)
                            .addComponent(checkRepetirColumnas))
                        .addGap(31, 31, 31)))
                .addComponent(tableScroll, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonAceptar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void buttonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAceptarActionPerformed

        //si se repite por filas y columnas
        if(checkRepetirColumnas.isSelected() && checkRepetirFilas.isSelected()){
            p.setkIguales(true);
            //Capurar el valor de las repeticiones de los Lambdas
            valorL = new Double[]{
                    Double.valueOf(tModelUnaL.getValueAt(0, 1).toString()),
                    Double.valueOf(tModelUnaL.getValueAt(0, 2).toString()),
                    Double.valueOf(tModelUnaL.getValueAt(0, 3).toString()),
                    Double.valueOf(tModelUnaL.getValueAt(0, 4).toString())
                };
            p.setValorL(valorL);
            valorK = Integer.valueOf((String)tModelUnaK.getValueAt(0, 1));
            p.setK(valorK);
            //MainController main = new MainController(valorK, valorL);
            dispose();
        }
        else if(checkRepetirColumnas.isSelected() == false && checkRepetirFilas.isSelected() == false){
            //Si no se repiten
            p.setkIguales(false);
            int[][] kDiferentes = new int[nFilas][mColumnas];
            
            int temp = 0;
            for (int i = 0; i < nFilas; i++) {
                for (int j = 0; j < mColumnas; j++) {
                    kDiferentes[i][j] = Integer.valueOf((String)tModelTodasK.getValueAt(temp++, 1));
                }    
            }
            kD = kDiferentes.clone();
            p.setkDiferentes(kDiferentes);

            temp = 0;
            ArrayList<Double> valorLdiferentes [][] = new ArrayList[nFilas][mColumnas];
            for (int i = 0; i < nFilas; i++) {
                for (int j = 0; j < mColumnas; j++) {
                    double value1 = Double.valueOf(tModelTodasL.getValueAt(temp, 1).toString());
                    double value2 = Double.valueOf(tModelTodasL.getValueAt(temp, 2).toString());
                    double value3 = Double.valueOf(tModelTodasL.getValueAt(temp, 3).toString());
                    double value4 = Double.valueOf(tModelTodasL.getValueAt(temp, 4).toString());
                    valorLdiferentes[i][j] = new ArrayList<Double>();
                    valorLdiferentes[i][j].add(value1); 
                    valorLdiferentes[i][j].add(value2); 
                    valorLdiferentes[i][j].add(value3); 
                    valorLdiferentes[i][j].add(value4); 
                    temp++;
                }    
            }
            valorLD = valorLdiferentes.clone();
            p.setValorLdiferentes(valorLdiferentes);
            
            dispose();
        }
        else if(checkRepetirColumnas.isSelected() && checkRepetirFilas.isSelected()==false){
            //Si no se repiten
            p.setkIguales(false);
            int[][] kDiferentes = new int[nFilas][mColumnas];
            
            for (int j = 0; j < mColumnas; j++) {
                for (int i = 0; i < nFilas; i++) {
                    kDiferentes[i][j] = Integer.valueOf((String)tModelColumnasK.getValueAt(j, 1));
                }    
            }
            kD = kDiferentes.clone();
            p.setkDiferentes(kDiferentes);
            
            ArrayList<Double> valorLdiferentes [][] = new ArrayList[nFilas][mColumnas];
            for (int j = 0; j < mColumnas; j++) {
                for (int i = 0; i < nFilas; i++) {
                    double value1 = Double.valueOf(tModelColumnasL.getValueAt(j, 1).toString());
                    double value2 = Double.valueOf(tModelColumnasL.getValueAt(j, 2).toString());
                    double value3 = Double.valueOf(tModelColumnasL.getValueAt(j, 3).toString());
                    double value4 = Double.valueOf(tModelColumnasL.getValueAt(j, 4).toString());
                    valorLdiferentes[i][j] = new ArrayList<Double>();
                    valorLdiferentes[i][j].add(value1); 
                    valorLdiferentes[i][j].add(value2); 
                    valorLdiferentes[i][j].add(value3); 
                    valorLdiferentes[i][j].add(value4); 
                }    
            }
            valorLD = valorLdiferentes.clone();
            p.setValorLdiferentes(valorLdiferentes);
                        
            dispose();
        }
        else if(checkRepetirColumnas.isSelected()==false && checkRepetirFilas.isSelected()){
            //Si no se repiten
            p.setkIguales(false);
            int[][] kDiferentes = new int[nFilas][mColumnas];
            
            for (int i = 0; i < nFilas; i++) {
                for (int j = 0; j < mColumnas; j++) {
                    kDiferentes[i][j] = Integer.valueOf((String)tModelFilasK.getValueAt(i, 1));
                }    
            }
            kD =kDiferentes.clone();
            p.setkDiferentes(kDiferentes);
        
            ArrayList<Double> valorLdiferentes [][] = new ArrayList[nFilas][mColumnas];
            for (int i = 0; i < nFilas; i++) {
                for (int j = 0; j < mColumnas; j++) {
                    double value1 = Double.valueOf(tModelFilasL.getValueAt(i, 1).toString());
                    double value2 = Double.valueOf(tModelFilasL.getValueAt(i, 2).toString());
                    double value3 = Double.valueOf(tModelFilasL.getValueAt(i, 3).toString());
                    double value4 = Double.valueOf(tModelFilasL.getValueAt(i, 4).toString());
                    valorLdiferentes[i][j] = new ArrayList<Double>();
                    valorLdiferentes[i][j].add(value1); 
                    valorLdiferentes[i][j].add(value2); 
                    valorLdiferentes[i][j].add(value3); 
                    valorLdiferentes[i][j].add(value4); 
                }    
            }
            valorLD = valorLdiferentes.clone();
            p.setValorLdiferentes(valorLdiferentes);
                        
            dispose();        
        }
            
    }//GEN-LAST:event_buttonAceptarActionPerformed
    
    
    private void checkRepetirFilasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkRepetirFilasActionPerformed

        if (checkRepetirFilas.isSelected() && !checkRepetirColumnas.isSelected()) {
            checkModelFilas();
        } else if (checkRepetirColumnas.isSelected() && !checkRepetirFilas.isSelected()) {
            checkModelColumnas();
        } else if (checkRepetirFilas.isSelected() && checkRepetirColumnas.isSelected()) {
            checkModelUnaPorTodas();
        } else {
            checkModelMostrarTodas();
        }
    }//GEN-LAST:event_checkRepetirFilasActionPerformed

    private void checkRepetirColumnasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkRepetirColumnasActionPerformed

        if (checkRepetirColumnas.isSelected() && !checkRepetirFilas.isSelected()) {
            checkModelColumnas();
        } else if (checkRepetirFilas.isSelected() && !checkRepetirColumnas.isSelected()) {
            checkModelFilas();
        } else if (checkRepetirFilas.isSelected() && checkRepetirColumnas.isSelected()) {
            checkModelUnaPorTodas();
        } else {
            checkModelMostrarTodas();
        }
    }//GEN-LAST:event_checkRepetirColumnasActionPerformed

    private void tableKLambdasFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tableKLambdasFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_tableKLambdasFocusLost

    private void radiobKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radiobKActionPerformed
        checkRepetirColumnasActionPerformed(null);
    }//GEN-LAST:event_radiobKActionPerformed

    private void radiobLambdaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radiobLambdaActionPerformed
        
        checkRepetirColumnasActionPerformed(null);
//
//        if(checkRepetirColumnas.isSelected() && checkRepetirFilas.isSelected()){
//            valorK = Integer.valueOf((String)tModelUnaK.getValueAt(0, 1));
//        }
//        else if(checkRepetirColumnas.isSelected() == false && checkRepetirFilas.isSelected() == false){
//            p.setkIguales(false);
//            int[][] kDiferentes = new int[nFilas][mColumnas];
//            
//            int temp = 0;
//            for (int i = 0; i < nFilas; i++) {
//                for (int j = 0; j < mColumnas; j++) {
//                    kDiferentes[i][j] = Integer.valueOf((String)tModelTodasK.getValueAt(temp++, 1));
//                }    
//            }
//            kD = kDiferentes.clone
//            p.setkDiferentes(kDiferentes);
//        }
//        else if(checkRepetirColumnas.isSelected() && checkRepetirFilas.isSelected()==false){
//            //Si no se repiten
//            p.setkIguales(false);
//            int[][] kDiferentes = new int[nFilas][mColumnas];
//            
//            for (int j = 0; j < mColumnas; j++) {
//                for (int i = 0; i < nFilas; i++) {
//                    kDiferentes[i][j] = Integer.valueOf((String)tModelFilasK.getValueAt(j, 1));
//                }    
//            }
//            p.setkDiferentes(kDiferentes);
//        }
//        else if(checkRepetirColumnas.isSelected()==false && checkRepetirFilas.isSelected()){
//            //Si no se repiten
//            p.setkIguales(false);
//            int[][] kDiferentes = new int[nFilas][mColumnas];
//            
//            for (int i = 0; i < nFilas; i++) {
//                for (int j = 0; j < mColumnas; j++) {
//                    kDiferentes[i][j] = Integer.valueOf((String)tModelFilasK.getValueAt(i, 1));
//                }    
//            }
//            p.setkDiferentes(kDiferentes);
//        }
//            

    }//GEN-LAST:event_radiobLambdaActionPerformed

    void checkModelColumnas() {
        String s[] = null;
        int col = 0;
    
        if(radiobLambda.isSelected()){
             s = new String[]{"Columna","Lambda 1", "Lambda 2",
                        "Lambda 3", "Lambda 4"};
             col = 5;
             tModelColumnasL = new DefaultTableModel(
                new Object[mColumnas][col],s);
             tableKLambdas.setModel(tModelColumnasL);

        }else{
            s = new String[]{
              "Columna","K"  
            };
            col = 2;
            tModelColumnasK = new DefaultTableModel(
                new Object[mColumnas][col],s);
            tableKLambdas.setModel(tModelColumnasK);

        }

        for (int i = 0; i < mColumnas; i++) {
            String celda = "(" + i + ")";
            tableKLambdas.setValueAt(celda, i, 0);
        }
    }

    void checkModelFilas() {
        String s[] = null;
        int col = 0;
        if(radiobLambda.isSelected()){
             s = new String[]{"Fila","Lambda 1", "Lambda 2",
                        "Lambda 3", "Lambda 4"};
             col = 5;
             tModelFilasL = new DefaultTableModel(
                new Object[nFilas][col],s);
             tableKLambdas.setModel(tModelFilasL);

        }else{
            s = new String[]{
              "Fila","K"  
            };
            col = 2;
            tModelFilasK = new DefaultTableModel(
                new Object[nFilas][col],s);
            tableKLambdas.setModel(tModelFilasK);

        }

        for (int i = 0; i < nFilas; i++) {
            String celda = "(" + i + ")";
            tableKLambdas.setValueAt(celda, i, 0);
        }
    }

    private void checkModelMostrarTodas() {
        String s[] = null;
        int col = 0;
        if(radiobLambda.isSelected()){
             s = new String[]{"Celda","Lambda 1", "Lambda 2",
                        "Lambda 3", "Lambda 4"};
             col = 5;
             tModelTodasL = new DefaultTableModel(
                new Object[nFilas*mColumnas][col],s);
             tableKLambdas.setModel(tModelTodasL);

        }else{
            s = new String[]{
              "Celda","K"  
            };
            col = 2;
            tModelTodasK = new DefaultTableModel(
                new Object[nFilas*mColumnas][col],s);
            tableKLambdas.setModel(tModelTodasK);

        }
        
        int aux = 0;
        for (int i = 0; i < nFilas; i++) {
            for (int j = 0; j < mColumnas; j++) {
                String celda = "(" + i + "," + j + ")";
                tableKLambdas.setValueAt(celda, aux, 0);
                aux++;
            }

        }
                
    }

    private void checkModelUnaPorTodas() {
        String s[] = null;
        int col = 0;
        if(radiobLambda.isSelected()){
             s = new String[]{"Celda","Lambda 1", "Lambda 2",
                        "Lambda 3", "Lambda 4"};
             col = 5;
             tModelUnaL = new DefaultTableModel(
                new Object[1][col],s);
             tableKLambdas.setModel(tModelUnaL);

        }else{
            s = new String[]{
              "Celda","K"  
            };
            col = 2;
            tModelUnaK = new DefaultTableModel(
                new Object[1][col],s);
            tableKLambdas.setModel(tModelUnaK);
        }
        
        tableKLambdas.setValueAt("Todas", 0, 0);

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAceptar;
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JCheckBox checkRepetirColumnas;
    private javax.swing.JCheckBox checkRepetirFilas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JRadioButton radiobK;
    private javax.swing.JRadioButton radiobLambda;
    private javax.swing.JTable tableKLambdas;
    private javax.swing.JScrollPane tableScroll;
    // End of variables declaration//GEN-END:variables
}
